import { defineStore } from 'pinia'
import { reqCategory1, reqCategory2, reqCategory3 } from '../../api/attr'
import { IResponseCategoryData } from '../../api/attr/type'

interface IAttrStore {
  category1: IResponseCategoryData[]
  category1Value: string
  category2: IResponseCategoryData[]
  category2Value: string
  category3: IResponseCategoryData[]
  category3Value: string
}
const useAttrStore = defineStore('attr', {
  state(): IAttrStore {
    return {
      category1: [],
      category1Value: '',
      category2: [],
      category2Value: '',
      category3: [],
      category3Value: '',
    }
  },
  actions: {
    async getCategory1() {
      try {
        let result = await reqCategory1()
        if (result.code == 200) {
          this.category1 = result.data
        }
      } catch (error) {
        console.log(error)
      }
    },
    async getCategory2() {
      try {
        let result = await reqCategory2(this.category1Value)
        if (result.code == 200) {
          this.category2 = result.data
          console.log(this.category2)
        } else {
        }
      } catch (error) {
        console.log(error)
      }
    },
    async getCategory3() {
      try {
        let result = await reqCategory3(this.category2Value)
        if (result.code == 200) {
          this.category3 = result.data
        } else {
        }
      } catch (error) {
        console.log(error)
      }
    },
    clearData(param: number) {
      if (param == 1) {
        this.category2Value = ''
        this.category3Value = ''
      } else {
        this.category3Value = ''
      }
    },
    clearState() {
      this.$reset()
    },
  },
  getters: {},
})

export default useAttrStore
